package com.xauto.pioneer.appuser.activity.mine.receipt;

import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.ReceiptInfo;
import com.xauto.pioneer.appuser.util.ToastUtil;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

/**发票信息录入
 * Created by Administrator on 2018/5/22.
 */

public class ReceiptInputActivity  extends BaseActivity{

    public static final String TAG = ReceiptInputActivity.class.getSimpleName();

    @Bind(R.id.ac_receipt_input_topBar)
    TopBar ac_receipt_input_topBar;
    @Bind(R.id.rg_receipt_type)
    RadioGroup rg_receipt_type;
    @Bind(R.id.tvPayNext)
    TextView tvSetReceipt;
    @Bind(R.id.et_receipt_companyname)
    EditText et_receipt_name;
    @Bind(R.id.et_receipt_num)
    EditText et_receipt_num;
    @Bind(R.id.et_receipt_addr)
    EditText et_receipt_addr;
    @Bind(R.id.et_receipt_tel)
    EditText et_receipt_tel;
    @Bind(R.id.et_receipt_bank)
    EditText et_receipt_bank;
    @Bind(R.id.et_receipt_mailaddr)
    EditText et_receipt_mailaddr;
    @Bind(R.id.et_receipt_other)
    EditText et_receipt_other;
    @Bind(R.id.recept_type_individual)
    RadioButton recept_type_individual;
    @Bind(R.id.recept_type_company)
    RadioButton recept_type_company;


    protected String receipt_name;
    protected String receipt_bank;
    protected String receipt_address;
    protected String receipt_phone;
    protected String receipt_type="0";
    protected String receipt_num;
    protected String receipt_mailaddr;
    protected String receipt_message;
    private ReceiptInfo receiptInfo;

    @Override
    protected int getContentView() {
        return R.layout.ac_receipt_input;
    }

    @Override
    protected void initView() {
        ac_receipt_input_topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        ac_receipt_input_topBar.setText(TopBar.TITLE, getString(R.string.receipt_input));
        ac_receipt_input_topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        ac_receipt_input_topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        rg_receipt_type.setOnCheckedChangeListener(listener);
    }

    @Override
    protected void initData() {
        getUserReceipt();
    }
   private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
           switch (group.getCheckedRadioButtonId()){
               case R.id.recept_type_individual:
                   receipt_type ="1";
                   break;
               case R.id.recept_type_company:
                   receipt_type ="2";
                   break;
           }
       }
   };
    @OnClick({R.id.tvPayNext})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvPayNext:
                receipt_name = et_receipt_name.getText().toString();
                receipt_num = et_receipt_num.getText().toString();
                receipt_address = et_receipt_addr.getText().toString();
                receipt_phone = et_receipt_tel.getText().toString();
                receipt_mailaddr = et_receipt_mailaddr.getText().toString();
                receipt_bank = et_receipt_bank.getText().toString();
                receipt_message = et_receipt_other.getText().toString();
                Log.e("tag",receipt_name);
               if(receipt_type=="0"){
                   ToastUtil.showShort(this,"请选择发票类型");
               }else if(receipt_name.length()<=0){
                   ToastUtil.showShort(this,"请填写发票抬头");
               }else if(receipt_num.length()<=0){
                   ToastUtil.showShort(this,"请填写纳税人识别号");
               }else if(receipt_address.length()<=0){
                   ToastUtil.showShort(this,"请填写地址");
               }else if(receipt_phone.length()<=0){
                   ToastUtil.showShort(this,"请填写电话");
               }else if(receipt_mailaddr.length()<=0){
                   ToastUtil.showShort(this,"请填写邮寄地址");
               }else if(receipt_bank.length()<=0){
                   ToastUtil.showShort(this,"请填写开户行及账号");
               }else{
                   //提交发票信息
                   SetUserReceipt();
               }
                break;
        }
    }
    /**
     * 提交发票信息
     * */
    private void SetUserReceipt() {
        new LHttpLib().SetReceiptInfo(mContext, memberId, receipt_name, receipt_bank,receipt_address,
                receipt_phone,receipt_type,receipt_num,receipt_message,receipt_mailaddr,new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                prompt("提交成功");
            }
        });
    }
    /**
     * 获取发票信息
     * */
    private void getUserReceipt() {
        new LHttpLib().getReceiptInfo(mContext, memberId, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                receiptInfo = new ReceiptInfo().parse(jsonStatus.data.optJSONObject("member_receipt_info"));
                if(receiptInfo!=null){
                    et_receipt_name.setText(receiptInfo.receipt_name);
                    et_receipt_addr.setText(receiptInfo.receipt_address);
                    et_receipt_bank.setText(receiptInfo.receipt_bank);
                    et_receipt_num.setText(receiptInfo.receipt_num);
                    et_receipt_tel.setText(receiptInfo.receipt_phone);
                    et_receipt_other.setText(receiptInfo.receipt_message);
                    et_receipt_mailaddr.setText(receiptInfo.receipt_mailaddr);
                    receipt_type = receiptInfo.receipt_type;
                    if(receipt_type.equals("1")){
                        recept_type_individual.setChecked(true);
                        recept_type_company.setChecked(false);
                    }else if(receipt_type.equals("2")){
                        recept_type_company.setChecked(true);
                        recept_type_individual.setChecked(false);
                    }
                }

            }
        });
    }
}
